import  javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
public class Main {
    public static void main(String[] args) {
        // LoanCalculate loanCalculator = new LoanCalculate(360,100,0.049);
        //  loanCalculator.avgCaption();
        //loanCalculator.avgInterest();
        // loanCalculator.print(2);
        //loanCalculator.print(1);

        JFrame frame = new JFrame("贷款计算器");
        frame.setLayout(new FlowLayout());

        frame.setSize(600, 400);

        JLabel labelAmount = new JLabel("贷款总额");
        JLabel labelPeriod = new JLabel("贷款期数");
        JLabel labelInterest = new JLabel("年利率");

        JTextField textAmount = new JTextField();
        textAmount.setColumns(10);
        JTextField textPeriod = new JTextField();
        textPeriod.setColumns(10);
        JTextField textInterest = new JTextField();
        textInterest.setColumns(10);

        JRadioButton radioBtnAvgInterest = new JRadioButton("等额本息");
        JRadioButton radioBtnAvgCaption = new JRadioButton("等额本金");

        JLabel labelTotalInterest = new JLabel("总利息");
        JTextField textTotalInterest = new JTextField(20);

        JButton btnCalculate = new JButton("计算");

        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(radioBtnAvgInterest);
        buttonGroup.add(radioBtnAvgCaption);

        frame.add(labelAmount);
        frame.add(textAmount);
        frame.add(labelPeriod);
        frame.add(textPeriod);
        frame.add(labelInterest);
        frame.add(textInterest);

        frame.add(radioBtnAvgInterest);
        frame.add(radioBtnAvgCaption);

        frame.add(labelTotalInterest);
        frame.add(textTotalInterest);

        frame.add(btnCalculate);

        frame.setVisible(true);

        btnCalculate.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //1.取出来
                String txtAmount = textAmount.getText();
                String txtPeriod = textPeriod.getText();
                String txtYearInterest = textInterest.getText();
                //2.计算
                LoanCalculate loanCalculate = new LoanCalculate(Integer.parseInt(txtAmount),
                        Integer.parseInt(txtPeriod), Double.parseDouble(txtYearInterest));
                double result;
                if (radioBtnAvgInterest.isSelected()) {
                    result = loanCalculate.calculateAvgInterest();
                } else {
                    result = loanCalculate.calculateAvgCap();

                }
                textTotalInterest.setText(String.valueOf(result));

            }
        });
    }
}